/**
 * created by 小卷毛, 2021-07-30
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.CouponData
import com.rzico.sbl.model.FilterData
import com.rzico.sbl.model.ResponseModel
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：
 * 作 者：小卷毛  2021-07-30 09:57
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class CouponViewModel : CommonViewModel() {

    val mTypeList by lazy {
        listOf(
            FilterData("满减", "fullcut"),
            FilterData("满折", "discount")
        )
    }

    val mRangeList by lazy {
        listOf(
            FilterData("全场", "all"),
            FilterData("店内", "shop"),
            FilterData("商城", "mall")
        )
    }

    /**
     * 优惠券列表
     */
    fun couponList(
        page: Int,
        onSuccess: (ArrayList<CouponData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<CouponData>>>(BaseUrl.mngCouponList)
            .params(
                getParams(
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<CouponData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

}